<?php include('header.php');?>
<?php
$group_detail_id = $_GET['id'];

if($_GET['action']=='edit') {
	
	if($_POST["roomtype_price"]!="" && $_POST["roomtype_id"]!= ""){
		
		for($i=0; $i<count($_POST['roomtype_price']); $i++){
			//mysql_query("UPDATE group_detail_room SET group_detail_room_price = '".$_POST['roomtype_price'][$i]."' WHERE group_detail_room_id = '".$_POST['group_detail_room_id'][$i]."' ");
			mysql_query("INSERT INTO group_detail_room (group_detail_id,roomtype_id,group_detail_room_price) VALUES ('".$_POST['group_detail_id']."','".$_POST['roomtype_id'][$i]."','".$_POST["roomtype_price"][$i]."') ON DUPLICATE KEY UPDATE group_detail_room_price='".$_POST["roomtype_price"][$i]."'");
		}
		echo "<script>window.location='".$_SERVER['PHP_SELF']."?id=".$_POST['group_detail_id']."'</script>";
	}

}

	$Query = mysql_query("SELECT roomtype_id,roomtype_name FROM roomtype ORDER BY roomtype_name ");
	
?>
<body>
<form action="?action=edit" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>จัดการราคาห้องพัก</h1>
      <div class="buttons"><a onclick="chk_form('formedit');" class="button"><span>บันทึก</span></a><a onclick="location = 'group_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
  
    <table class="list">
    	<thead>
    	<tr>
    		<td class="center">ลำดับ</td>
    		<td class="center">ชื่อห้อง</td>
    		<td class="center">ราคา</td>
    	</tr>
    	</thead>
<?php 
	$num=0;
	while($Row = mysql_fetch_array($Query)){
		$num++;
		$gQuery = mysql_query("SELECT * FROM group_detail,group_detail_room WHERE group_detail.group_detail_id = group_detail_room.group_detail_id AND group_detail_room.group_detail_id = '".$_GET["id"]."' AND group_detail_room.roomtype_id = '".$Row['roomtype_id']."' ");
		$gRow = mysql_fetch_array($gQuery);
?>
       <tr>
        <td class="center"><?php echo $num;?></td>
        <td class="center"><?php echo $Row['roomtype_name'];?></td>
        <td class="center"><input name="roomtype_price[]" type="text" style="text-align: right;" maxlength="100" value="<?php if($gRow['group_detail_room_price']!=""){ echo $gRow['group_detail_room_price'];}else{echo "0";}?>" /><input type="hidden" name="roomtype_id[]" value="<?php echo $Row['roomtype_id']; ?>"/></td>
      </tr>
<?php } ?>
    </table>
    <input type="hidden" name="group_detail_id" value="<?php echo $group_detail_id;?>"/>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</form>
<?php mysql_close(); ?>
</body>
</html>
